<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>iterator</title>
</head>

<body>
    <ul>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
    <script>
        /*
        * Iterator
        *   是一种接口机制,  为各种不同的数据结构提供统一的访问机制;
        *
        * //作用:
        * //为各种数据结构, 提供一个统一的访问接口
        * //使数据结构的成员能够按照某种次序排列.
        * //for of 主要就是为了遍历iterator
        * */

        //    Promise.all()

        let arr = ["a", "b", "c"];
        {
            function makeIterator(arr) {
                let nextIndex = 0;
                return {
                    next: function () {
                        return nextIndex < arr.length ? {
                            value: arr[nextIndex++],
                            done: false
                        } : {
                                value: undefined,
                                done: true
                            }
                    }
                };
            }
            let it = makeIterator(arr);
            //    it.next()
            console.log(it);//
            console.log(it.next());//{value: "a", done: false}
            console.log(it.next());//{value: "b", done: false}
            console.log(it.next());//{value: "c", done: false}
            console.log(it.next());//{value: undefined, done: true}

            //next()方法返回一个对象, 表示当前成员的数据
            //对象中包含两个属性, value表示值, done表示是否结束

        }

        {

            console.log(arr);
            let it = arr[Symbol.iterator]();//数组内部本身就内置了iterator接口
            console.log(it);// Array Iterator {}
            console.log(it.next());//{value: "a", done: false}
            console.log(it.next());//{value: "b", done: false}
            console.log(it.next());//{value: "c", done: false}
            console.log(it.next());//{value: undefined, done: true}

            for (let key of arr) {
                console.log(key)
            }

            let obj = {
                a: 1,
                b: 2
            };
            let keys = Object.keys(obj);
            console.log(keys);
            for (let key of keys) {//obj is not iterable
                console.log(key)
            }
        }
        {
            let map = new Map();
            map.set("name", "zhuangsan");
            map.set("name2", "lisi");

            console.log(map);
            let it = map[Symbol.iterator]();
            console.log(it.next());
            console.log(it.next());
            console.log(it.next());
        }

        {
            //        String
            //        Array
            //        Map
            //        类数组
            //        arguments
            //        NodeList
            function args() {
                console.log(arguments)
            }
            args();
            let liList = document.querySelectorAll("li");
            console.log(liList);

            let str = "123456";
            //        let str = 1234; //str is not iterable
            for (let key of str) {
                console.log(key)
            }

        }
    </script>
</body>

</html>